import pystray
from PIL import Image

class WinPystray:

    def on_quit_clicked(self, icon, item):
        icon.stop()

    def on_run_clicked(self, icon, item):
        print("运行")
        self.startInvoke()

    def on_stop_clicked(self, icon, item):
        print("停止")
        self.stopInvoke()

    def start(self,startInvoke,stopInvoke):
        image = Image.open("app.ico")
        icon = pystray.Icon("name", image, "web机器人")

        self.startInvoke = startInvoke
        self.stopInvoke = stopInvoke
        # 创建菜单选项
        menu = (pystray.MenuItem("运行", self.on_run_clicked),
                pystray.MenuItem("停止", self.on_stop_clicked),
                pystray.MenuItem("退出", self.on_quit_clicked),)

        # 将菜单选项添加到图标对象
        icon.menu = menu

        # 开始运行图标
        icon.run()
